package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: Learner
 * @Version: 1.0
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> findByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> byPage = nursingProjectMapper.findByPage(name, status);
        return PageResponse.of(byPage, NursingProjectVo.class);

    }

  /*  //老师的方法
    @Override
    public PageResponse<NursingProjectVo> findByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> byPage = nursingProjectMapper.findByPage(name, status);


        List<NursingProjectVo> list = byPage.getResult();

        List<Long> ids = list.stream().map(vo -> vo.getId()).collect(Collectors.toList());

        List<Map> mapList= nursingProjectMapper.queryCountByProjectIds(ids);

        Map<Long,Long> map =mapList.stream().collect(Collectors.toMap(m->Long.valueOf(m.get("pid").toString()),
                m->Long.valueOf(m.get("ct").toString())));

        for(NursingProjectVo vo:list){
            vo.setCount(map.getOrDefault(vo.getId(),0L));
        }

        return PageResponse.of(byPage, NursingProjectVo.class);

    }*/

    /*
     * @Date 2024/6/8 8:21
     *新增护理项目
     */
    @Override
    public Integer addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto, nursingProject);

        try {
            return nursingProjectMapper.insert(nursingProject);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_INSERT_FAIL);
        }

    }

    @Override
    public NursingProjectVo findById(long id) {
        NursingProjectVo nursingProjectVo = new NursingProjectVo();
        NursingProject nursingProject = nursingProjectMapper.findById(id);
        BeanUtils.copyProperties(nursingProject, nursingProjectVo);
        return nursingProjectVo;

    }

    @Override
    public void updateNursing(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject=new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto,nursingProject);
        try {
            nursingProjectMapper.updateNursing(nursingProject);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_UPDATE_FAIL);
        }
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id, status);
    }

    @Override
    public void delNursing(long id) {
        try {
            nursingProjectMapper.delNursing(id);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_UPDATE_FAIL);

        }
    }

    @Override
    public List<NursingProjectVo> allNursing() {
        return nursingProjectMapper.allNursing();
    }
}
